package com.example.esdemo.service;

import cn.hutool.core.date.DateUtil;
import com.example.esdemo.dao.NewsRepository;
import com.example.esdemo.pojo.News;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class NewsServiceImpl implements NewsService {
    @Autowired
    private NewsRepository newsRepository;
    @Override
    public List<News> findLikeContent(String content) {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.queryStringQuery(content).field("content"));

//        WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("content", content);
        Iterable<News> search = newsRepository.search(queryBuilder);
        return parseNewsList(search);
    }

    @Override
    public List<News> findBetweenRecordDate(Date startDate, Date endDate) {

        BoolQueryBuilder queryBuilder = QueryBuilders
                .boolQuery()
                .must(QueryBuilders
                        .rangeQuery("record_date")
                        .from(startDate)
                        .to(endDate)
                );
        Iterable<News> search = newsRepository.search(queryBuilder);

        return parseNewsList(search);
    }

    @Override
    public List<News> findBySource(String source) {
        NativeSearchQuery nativeSearchQuery=new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(source).defaultField("source"))
                .withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
                .build();
        Page<News> search = newsRepository.search(nativeSearchQuery);
        return search.getContent();
    }

    @Override
    public List<News> findByAuthor(String author) {
        NativeSearchQuery nativeSearchQuery=new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(author).defaultField("author"))
                .withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
                .build();
        Page<News> search = newsRepository.search(nativeSearchQuery);
        return search.getContent();
    }

    @Override
    public Integer countBySource(String source) {
        NativeSearchQuery nativeSearchQuery=new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(source).defaultField("source"))
                .withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
                .build();
        QueryBuilders.termQuery("source",source);
        return null;
    }

    @Override
    public void save(News news) {
        newsRepository.save(news);
    }

    private List<News> parseNewsList(Iterable<News> search){
        List<News> newsList=new ArrayList<>();
        for (News news : search) {
            newsList.add(news);
        }
        return newsList;
    }


}
